//
//  ZNewNotifyVC.m
//  WeClubs
//
//  Created by Zed Link on 17/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZNewNotifyVC.h"
#import "ZMemberChoseVC.h"
#import "ZClubsListVC.h"

@interface ZNewNotifyVC ()

//存放所有学生Id数组
@property(nonatomic)NSMutableArray *sudentsDataArr;
//用来存储发送内容
@property(nonatomic)NSString  *content;
@end

static NSString *ZNewMemberGroupCellID = @"ZNewMemberGroupCellID";
@implementation ZNewNotifyVC

#pragma mark - Life Cycle
- (void)viewDidLoad{

    [super viewDidLoad];
    
    [self.mainView registerNib:[UINib nibWithNibName:@"ZNewMemberGroupCell" bundle:nil] forCellReuseIdentifier:ZNewMemberGroupCellID];
}
#pragma mark - setupNav
- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"通知发布";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"发布" andSelector:@selector(newNotifyAction)];
}


#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.interfaceConfig.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 2) {
        if (self.dataArr.count > 0) {
            return 1;
        }else{
            return 0;
        }
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 2) {
        if (self.dataArr.count >0) {
            return 120;
        }else{
            return 0;
        }
    }else if (indexPath.section ==0){
    
        return 200;
    }
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

            switch (indexPath.section) {
            case ZNewCellTypeTextField:{
                ZNewTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NewTextViewCellReuseID];
                [cell setPlaceHolderText:@"请输入通知内容"];
                
                Z_WEAK_SELF;
                cell.textViewTextHandlerCallBack =^(NSString *textF){
                    
                    weakSelf.content  = textF;
                };
                
                return cell;
            }
                break;
            case ZNewCellTypeMemberChose:{
                ZNewMemberChoseCell *cell = [tableView dequeueReusableCellWithIdentifier:NewMemberChoseCellReuseID];
                return cell;
            }
            case 2:{
                    if (self.dataArr.count>0) {
                        ZNewMemberGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:ZNewMemberGroupCellID];
                        cell.nameLabel.text = _club_name;
                        cell.dataArr = self.dataArr;
                        cell.countLabel.text = [NSString stringWithFormat:@"%zd人",_dataArr.count];
                        cell.backgroundColor = [UIColor redColor];
                        return cell;
                        }else{
                        return nil;
                    }
             }
                break;
            default:
                return nil;
                break;
        }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 0) {
        // 选择社团成员
        ZMemberChoseVC *vc = [ZMemberChoseVC zNew];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - SEL Methods and loadData
- (void)newNotifyAction{

    [MBProgressHUD zAlertProgress:@"正在加载中"];
    
    NSString *students = [self.sudentsDataArr componentsJoinedByString:@","];
 
    NSDictionary *params = @{
                             @"content" :_content,
                             @"club_id" :@(self.club_id),
                             @"students" : students
                             };
    
    [HTTPRequest POST:kPublic_notifyUrl parameter:params success:^(id resposeObject) {
        
        [MBProgressHUD zAlertHideHUD];
        
        if ([resposeObject[@"result_code"] integerValue] == ZNetManagerStatusSuccess) {
       
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            
            [MBProgressHUD zAlertMessage:resposeObject[@"result_msg"] to:self.view afterDelay:1.0];
        }
    } failure:^(NSError *error) {
        
        [MBProgressHUD zAlertHideHUD];
    }];
    
}
#pragma mark - Setter && Getter Methods
- (void)setDataArr:(NSMutableArray *)dataArr{
    
    _dataArr = dataArr;
    
    [_dataArr enumerateObjectsUsingBlock:^(ZStudentModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [self.sudentsDataArr addObject:model.student_id];
    }];
    [self.mainView reloadData];
}

- (NSMutableArray *)sudentsDataArr{
    
    if (!_sudentsDataArr) {
        _sudentsDataArr =[NSMutableArray array];
    }
    return _sudentsDataArr;
    
}
@end
